package net.towee.server.authentication;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import net.towee.server.util.RandomBase64Generator;

/**
 * LoginzaUser POJO representation of received JSON
 * data.
 * @author c58
 */
public class LoginzaResponse {
	private String identity;
	private String provider;
	private String language;
	
	private String email;
	private String nickname;
	private String gender;
	private String dob;
	
	private Map<String, String> name;
	
	private String error_type;
	private String error_message;

	public LoginzaResponse() {}

	public String getLanguage() {
		return language;
	}
	
	public String getIdentify() {
		return identity;
	}

	public String getEmail() {
		return email;
	}

	public String getProvider() {
		return provider;
	}

	public String getNickname() {
		if(nickname == null || nickname.trim().equals(""))
			nickname = "noname_"+new RandomBase64Generator().next(5);
		
		return nickname;
	}

	public String getGender() {
		return gender;
	}

	public Date getDob() {
		if(dob == null)
			return null;
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			return df.parse(dob);
		} catch (ParseException e) {
			return null;
		}
	}

	public String getErrorType(){
		return this.error_type;
	}
	
	public String getErrorMessage(){
		return this.error_message;
	}	
	
	public String getFirstName() {
		if(name == null)
			return "";
		
		if (name.containsKey("first_name"))
			return name.get("first_name");

		if (name.containsKey("full_name")
				&& name.get("full_name").contains(" "))
			return name.get("full_name").split(" ")[0];

		return "";
	}

	public String getLastName() {
		if(name == null)
			return "";
		
		if (name.containsKey("last_name"))
			return name.get("last_name");

		if (name.containsKey("full_name")
				&& name.get("full_name").contains(" "))
			return name.get("full_name").split(" ")[1];

		return "";
	}
}